package com.hwua.view;

import com.hwua.entity.User;
import com.hwua.service.UserService;
import com.hwua.service.impl.UserServiceImpl;
import com.hwua.tqm.FatherLogin;
import com.hwua.tqm.Theme;

import java.util.Random;
import java.util.Scanner;

/**
 * @author CQY
 * 登录
 */
public class LoginView {

    public static void main(String args[]) {
        Theme.useTheme(6);
        Random ran=new Random();
        //创建登录界面
        FatherLogin frame = new FatherLogin() {
            @Override
            public String randomCode() {
                //使用随机数生成验证码

                //可以随机生成大小写字母
                //或者数字
                //建议4-6位
                return randomNum();
            }

            public String randomString(){
                String s="";
                char [] c=new char[4];
                char k='a';
                for (int i = 0; i < 4; i++) {
                    c[i]=(char)(ran.nextInt(25)+k);
                    s=s+c[i];
                }
                return s;
            }
            public String randomNum(){
                int m=ran.nextInt(9000)+1000;
                return m+"";
            }

            @Override
            public boolean loginSubmit(String code, String inputCode, String username, String password) {
                //1.验证验证码是否正确
                //2.正确则判断用户名和密码是否正确 不正确返回false
                //如果验证码错误，终止登录

                UserService service = new UserServiceImpl();
                User user = null;
                user = service.findByName(username);
                if (!code.equalsIgnoreCase(inputCode)) {
                    System.out.println("验证码错误");
                    return false;
                } else
                    if (!user.getAdmin_pwd().equals(password) || user.getAdmin_pwd() == null || user.getAdmin_pwd() == "") {
                        System.out.println("密码错误");
                        return false;
                    } else if (user.getAdmin_name().equals(username) && user.getAdmin_pwd().equals(password)) {
                        System.out.println("登录成功");
                        StartView st=new StartView(username);
                        return true;
                    }
                    System.out.println("code = " + code);
                    System.out.println("inputCode = " + inputCode);
                    System.out.println("username = " + username);
                    System.out.println("password = " + password);
                    return false;
                }
        };

    }
}
